<?php

/*
  
  Project: CI Forum
  Type: PHP and database
  Desc: Contains all functions related to categories.
  Author: Yash Bharadwaj
 
  Function name : 
  view_forum     -> get value from database and show threads inside that forum.
  view_category  -> get value from database and show forums inside that category.
  view_thread    -> get value from database and show thread details along with posts.
  

*/

class Viewmodel extends Model {

    function Viewmodel() {
    // Call the Model constructor
        parent::Model();
        $this->load->helper('date');
        $this->load->library('table');

    }

    function view_all() {

        $query = $this->db->get('board_categories');
        $categories = $query->result_array();

        if(empty($categories)) {	$out="Forum is empty. No category exist."; return;	}

        $out="";
        for($m=0;$m<count($categories);$m++) {

            $this->db->where('cat_title',$categories[$m]['url_title']);
            $query = $this->db->get('board_forums');
            $forums = $query->result_array();


            if(empty($forums)) { continue;	}



            $out.= '<table width="100%" border="1">
					<thead>
						<tr>
						<th colspan="4">
							<h1>'.anchor('board/view_category/'.$categories[$m]['url_title'],$categories[$m]['category_title']).'<h1>
						</th>
						</tr>
					</thead>
					<tbody><tr>
						<th width="40%"><h3>Forum Name</h3></td>
						<th width="10%"><h3>Topics</h3></td>
						<th width="10%"><h3>Replies</h3></td>
						<th><h3>Latest post info</h3></td>
					</tr>';



            for($i=0;$i<count($forums);$i++) {

            //we are calculating total number of thread for a given forum title
                $this->db->where('forum_title',$forums[$i]['url_title']);
                $query = $this->db->get('board_threads');
                $topics = $query->num_rows();

                $this->db->where('forum_title',$forums[$i]['url_title']);
                $query = $this->db->get('board_posts');
                $posts = $query->num_rows();


                $this->db->order_by('latest_entry_date','desc');
                $this->db->limit(1);
                $this->db->where('forum_title',$forums[$i]['url_title']);
                $query = $this->db->get('board_threads');
                $threads = $query->row_array();

                $out.= '<tr>
								<td>'.anchor('board/view_forum/'.$forums[$i]['url_title'],$forums[$i]['forum_title']).'<br />'.$forums[$i]['forum_desc'].'</td>
								<td>'.$topics.'</td>
								<td>'.$posts.'</td>
								<td>'; 
                if(!empty($threads))
                    $out.= anchor('board/view_thread/'.$forums[$i]['url_title']."/".$threads['url_title'],$threads['thread_title']);
                $out.= '&nbsp;</td>
						  </tr>
						  <tr>
								<td colspan="4">&nbsp;</td>
						  </tr>';
            //print_r($forums);

            }
            $out.= '</tbody>
				<tfoot>
				<td colspan="4">&nbsp;</td>
				</tfoot>
				</table><br />
				';

        }

        return $out;

    }


    function view_category($cat_title) {
        $this->db->where('url_title',$cat_title);
        $query = $this->db->get('board_categories');
        $categories = $query->row_array();

        $out= '<table width="100%" border="1">
					<thead><tr>
						<th colspan="4"><h1>'.anchor('board/view_category/'.$categories['url_title'],$categories['category_title']).'<h1></td>
					</tr>
					</thead>
					<tbody><tr>
						<th width="40%"><h3>Forum Name</h3></td>
						<th width="10%"><h3>Topics</h3></td>
						<th width="10%"><h3>Replies</h3></td>
						<th><h3>Latest post info</h3></td>
					</tr>';

        $this->db->where('cat_title',$categories['url_title']);
        $query = $this->db->get('board_forums');
        $forums = $query->result_array();

        if(empty($forums)) {  //continue;
        //exit;
            return;
        }

        for($i=0;$i<count($forums);$i++) {

        //we are calculating total number of thread for a given forum title
            $this->db->where('forum_title',$forums[$i]['url_title']);
            $query = $this->db->get('board_threads');
            $topics = $query->num_rows();

            $this->db->where('forum_title',$forums[$i]['url_title']);
            $query = $this->db->get('board_posts');
            $posts = $query->num_rows();


            $this->db->order_by('latest_entry_date','desc');
            $this->db->limit(1);
            $this->db->where('forum_title',$forums[$i]['url_title']);
            $query = $this->db->get('board_threads');
            $threads = $query->row_array();


            $out.= '<tr>
								<td>'.anchor('board/view_forum/'.$forums[$i]['url_title'],$forums[$i]['forum_title']).'<br />'.$forums[$i]['forum_desc'].'</td>
								<td>'.$topics.'</td>
								<td>'.$posts.'</td>
								<td>'; 
            if(!empty($threads))
                $out.= anchor('board/view_thread/'.$forums[$i]['url_title']."/".$threads['url_title'],$threads['thread_title']);
            $out.= '&nbsp;</td>
						  </tr>
						  <tr>
								<td colspan="4">&nbsp;</td>
						  </tr>';
        //print_r($forums);

        }
        $out.= '</tbody>
				<tfoot>
				<td colspan="4">&nbsp;</td>
				</tfoot>
				</table><br />
				';


        return $out;



    }

    function view_forum($forum_title) {
        $this->db->where('url_title',$forum_title);
        $query = $this->db->get('board_forums');
        $forums = $query->row_array();


        $this->db->order_by('latest_entry_date','desc');
        $this->db->where('forum_title',$forum_title);
        $query = $this->db->get('board_threads');
        $threads = $query->result_array();

        $out= '<table width="100%" border="1">
					<thead>
					<tr>
						<th colspan="4"><h2>'.$forums['forum_title'].'<h2></th>
					</tr>
					</thead>
					<tr>
						<td>Topic Title</td>
						<td>Replies</td>
						<td>Views</td>
						<td>Latest post info</td>
					</tr>
					';


        for($i=0;$i<count($threads);$i++) {
            $this->db->where('thread_title',$threads[$i]['url_title']);
            $query = $this->db->get('board_posts');
            $posts_count = $query->num_rows();

            $this->db->order_by('posted_date','desc');
            $this->db->limit(1);
            $this->db->where('thread_title',$threads[$i]['url_title']);
            $query = $this->db->get('board_posts');
            $posts = $query->row_array();



            $out.= '<tr>
								<td>'.anchor('board/view_thread/'.$forum_title."/".$threads[$i]['url_title'],$threads[$i]['thread_title']).'</h3><br />'.$threads[$i]['thread_desc'].'</td>	
								<td>'.$posts_count.'</td>
								<td>'.$threads[$i]['thread_view'].'</td>
								<td>'; 
            if(!empty($posts))
                $out.= anchor('board/view_thread/'.$forum_title."/".$threads[$i]['url_title'],standard_date('DATE_RFC850',$posts['posted_date']));
            $out.= '&nbsp;
								
								</td>
						  </tr>
						  <tr>
								<td colspan="4">&nbsp;</td>
						  </tr>';
        //print_r($forums);

        }
        $out.= '<tfoot>
					<tr>
						<td colspan="4">&nbsp;</td></tr>
					</tfoot>
				</table>';



        return $out;



    }

    function view_thread($forum_title,$thread_title) {
    
        $this->db->where('url_title',$thread_title);
        $query = $this->db->get('board_threads');
        $read_thread = $query->row_array();
        $view=$read_thread['thread_view']+1;

        $this->db->where('url_title',$thread_title);
        $qc = $this->db->update('board_threads',array('thread_view'=>$view));

        $this->db->where('url_title',$thread_title);
        $query = $this->db->get('board_threads');
        $threads = $query->row_array();
        
        $this->db->where('thread_title',$thread_title);
        $query = $this->db->get('board_posts');
        $posts = $query->result_array();
        

        $out= '<table width="100%" border="1">
					<thead><tr><th colspan="2">Topic:'.$threads['thread_title'].'</th></tr></thead>
					<tfoot><td><td colspan="2">&nbsp;</td></tr></tfoot>
					<tbody>
					
						<td height="100px" width="20%"  valign="top">Name:</td>
						<td height="100px" valign="top">'.$threads['thread_content'].'</td>
						
					</tr>
					<tr>
								<td colspan="4" height="10px">&nbsp;</td>
						  </tr>';

        for($i=0;$i<count($posts);$i++) {
        
            $out.= '<tr>
                        <td height="100px" width="20%"></td>
			<td height="100px">'.$posts[$i]['post_content'].'</td>
                    </tr>
                    <tr>
			<td colspan="4" height="10px">&nbsp;</td>
                    </tr>';
        }
        $out.= '</tbody></table>';


        return $out;




    }

    function board_stat() {
    //calulate all post and threads ,also members
    //find latest member also.
        $query = $this->db->get('board_threads');
        $data['threads'] = $query->num_rows();

        $query = $this->db->get('board_posts');
        $data['posts'] = $query->num_rows();

        $query = $this->db->get('users');
        $data['users'] = $query->num_rows();

        $this->db->order_by('created','desc');
        $this->db->limit(1);
        $query = $this->db->get('users');
        $user = $query->row_array();
        $data['username']=$user['username'];
        $data['userid']=$user['id'];




        return $this->load->view('board/template/board_stat',$data,TRUE);


    }



}?>